﻿package com.teaphy.coroutinecrawler.build_coroutine

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.consumeEach
import kotlinx.coroutines.channels.produce

fun main() = runBlocking {
    println("start")

    val numChannel: ReceiveChannel<Int> = produce<Int> {
        var num = 0
        while (num < 10) {
            delay(200)
            send(num)
            num++
        }
    }

    // 开启一个协程接收数据
    launch {
        numChannel.consumeEach {
            println("接收到数据：$it")
        }
    }
    println("end")
}